<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="main :: common_head">
</head>
<style>
    .mylabel {
        display: inline;
    }

    .greenColor {
        color: green;
    }

    .redColor {
        color: red;
    }
</style>
<body>
<!-- 删除提示模态框 -->
<div class="modal fade" id="delcfmModel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="myModalLabel">提示</h4>
            </div>
            <div class="modal-body">是否要删除？</div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="deleteProperty()">确定</button>
            </div>
        </div>/.modal-content
    </div>/.modal-dialog
</div>
<!-- 删除提示模态框结束 -->

<div class="container-fluid-full">
    <div class="row-fluid">

        <noscript>
            <div class="alert alert-block span10">
                <h4 class="alert-heading">Warning!</h4>
                <p>You need to have <a href="" target="_blank">JavaScript</a> enabled to use this site.</p>
            </div>
        </noscript>

        <!-- start: Content -->
        <div id="content" class="span10" style="width: 100%">
            <ul class="breadcrumb" th:if="${#request.getParameter('breadcrumb') == 'true'}">
                <li>
                    <i class="fas fa-circle-notch"></i>
                    <a id="home" href="#" onclick="parent.location.reload()">主页</a>
                    <i class="icon-angle-right"></i>
                </li>
                <li><a href="#">能源属性</a></li>
            </ul>
            <div class="row-fluid sortable">
                <div class="box span12">
                    <div class="box-header" data-original-title>
                        <h2><i class="halflings-icon white edit"></i><span class="break"></span>添加能源属性</h2>
                    </div>
                    <div class="box-content">
                        <form id="form1" class="form-horizontal" enctype="multipart/form-data" method="post">
                            <input type="hidden" id="typeId" name="type_id">
                            <fieldset>
                                <div class="control-group">
                                    <label class="control-label">能源属性名称</label>
                                    <div class="controls">
                                        <input class="input-xlarge focused" type="text" placeholder=""
                                               name="typeName" onchange="checkPropertyName()" required="required"
                                               id="typeName">
                                        <div style="display: inline" id="checkName">
                                        </div>
                                        <span style="color:red;font-size: 20px">*</span>
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">能源属性图片</label>
                                    <div class="controls">
                                        <input class="input-xlarge focused" type="file" id="icon"
                                               accept="image/jpg,image/jpeg,image/png,image/PNG" placeholder=""
                                               name="icon">
                                        <span style="color:red;font-size: 20px">*</span>
                                    </div>
                                </div>
                                <div class="form-actions">
                                    <button type="submit" class="btn btn-primary"
                                            onclick="toAddProperty();return false">提交
                                    </button>
                                    <button type="button" class="btn btn-primary" onclick="toDevice()">返回</button>
                                </div>
                            </fieldset>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="clearfix"></div>

<div th:replace="main::common_js"></div>

<script>
    breadcrumb()
    window.onload = function() {
        parent.modifyTitle('能源属性')
    }
</script>

<script th:src="@{/js/detect-element-resize.js}"></script>
<script th:src="@{/js/iframe-resize.js}"></script>

<script>
    var addCheckName = true;

    function toAddProperty() {
        var formData = new FormData($("#form1")[0]);
        var typeName = document.getElementById("typeName");
        var fileInput = $('#icon').get(0).files[0];

        console.log(formData)
        if (!fileInput) {
            window.parent.layerAlert('属性图片不能为空', {
                icon: 5,
                title: "提示"
            });
            return false;
        }
        if (typeName.value == "") {
            window.parent.layerAlert('属性名不能为空', {
                icon: 5,
                title: "提示"
            });
            return false;
        }
        if (addCheckName == false) {
            window.parent.layerAlert('请输入未被使用的属性名', {
                icon: 5,
                title: "提示"
            });
            return false;
        }
        $.ajax({
            type: "POST",
            data: formData,
            url: "/multipower/energyType/addEnergyType",
            processData: false,
            contentType: false,
            success: function (data) {
                console.log("成功");
                window.location.href = "/multipower/energyType/viewEnergyType";
            },
        })
    }

    function checkPropertyName() {
        var typeName = $("#typeName").val();
        console.log(typeName);
        $.ajax({
            type: "GET",
            data: {energyTypeName: typeName},
            url: "/multipower/energyType/check",
            success: function (data) {
                console.log(data.check);
                if (data.check == "yes") {
                    var span = document.createElement("span");
                    span.setAttribute("class", "greenColor");
                    span.innerHTML = "该属性名可以使用";
                    $("#checkName").html(span);
                    addCheckName = true;
                }
                if (data.check == "no") {
                    var span = document.createElement("span");
                    span.setAttribute("class", "redColor");
                    span.innerHTML = "该属性名已存在，请使用其他设备名";
                    $("#checkName").html(span);
                    addCheckName = false;
                }
            },
        })
    }
</script>
</body>
</html>
